<?php defined('SYSPATH') or die('No direct script access.');

//require Kohana::find_file('vendor', 'tcpdf/config/lanf/pol','php');
require Kohana::find_file('vendor', 'tcpdf/tcpdf','php');

class MypdfColumn {
    public $name;
    
    public $label;
    public $type;
    public $width;
    public $align_header;
    public $align_body;
    
    public $data;
    
    private $_curDataIndex = 0;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function __call($methodName, $args){
        if(strstr($methodName, 'set_') && count($args)==1){
            $varname = explode('_', $methodName);
            $varname = $varname[1];
            $this->$varname = $args[0];
        }
        return $this;
    }
    
    public function getDataFromDataSet($dataSet, $keyName){
        $data = array();
        foreach($dataSet as $k=>&$row){
            if(!isset($row[$keyName])){
                throw new Exception('MypdfColumn::getDataFromDataSet: column not found in dataset');
            }
            $data[] = $row[$keyName];
        }
        unset($row);
        
        $this->data = $data;
        
        return $this;
    }
    
    public function getData(){
        return $this->data[$this->_curDataIndex];
    }
    
    public function nextData(){
        ++$this->_curDataIndex;
        return $this;
    }
    
    protected function getCell(){
        $cell = new MypdfCell();
        $cell->w = $this->width;
        $cell->txt = ($this->label?$this->label:$this->name);
        $cell->set_align($this->align_header);
        $cell->set_border('');
        
        return $cell;
    }
    
    public function getHeaderCell(){
        $cell = $this->getCell();
        return $cell;
    }
    
    public function getBodyCell(){
        $cell = $this->getCell();
        $cell->txt = $this->getData();
        return $cell;
    }
}

class MypdfCell{
    // $w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M'
    // $pdf->Cell(40, 6, 'aaa', 'LR', 0, 'L', $fill);
    public $w=40;
    public $h=6;
    public $txt='<txt>';
    public $border=0;
    public $ln=0;
    public $align='L';
    public $fill=false;
    public $link='';
    public $stretch=0;
    public $ignore_min_height=false;
    public $calign='T';
    public $valign='M';
    
    public function __call($methodName, $args){
        if(strstr($methodName, 'set_') && count($args)==1){
            $varname = explode('_', $methodName);
            $varname = $varname[1];
            $this->$varname = $args[0];
        }
        return $this;
    }
    
    /**
     *
     * @param string $align left, center, right
     */
    public function set_align($align){
        $al;
        switch(strtolower($aling)){
            case'left':
                $al = 'L';
                break;
            case'center':
                $al = 'C';
                break;
            case'right':
                $al = 'R';
                break;
            default:
                $al = $align;
        }
        $this->align = $al;
        return $this;
    }
    
    public function set_border($style){
        $border = array(
            'LTRB' => array(
                'width' => 0.2, 
                'cap' => 'butt', 
                'join' => 'miter', 
                'dash' => 0, 
                'color' => array(0, 0, 0),
            )
        );
        $this->border = $border;
        return $this;
    }
    
//    public function setFromColumn(MypdfColumn $column){
//    }
    
    public function returnParameter(){
        return array($this->w, $this->h, $this->txt, $this->border, $this->ln, $this->align, $this->fill, $this->link, $this->stretch, $this->ignore_min_height, $this->calign, $this->valign);
    }
    
    public function addToPdf(TCPDF $pdf){
        $pdf->cell($this->w, $this->h, $this->txt, $this->border, $this->ln, $this->align, $this->fill, $this->link, $this->stretch, $this->ignore_min_height, $this->calign, $this->valign);
        return $this;
    }
}

class Mypdf extends TCPDF {
    
    public function addCell(MypdfCell $cell){
        $cell->addToPdf($this);
    }
    
    protected function simpleTableHeader($columnSet){
        foreach($columnSet as $col){
//            $this->Cell($w[$i], 7, $col->label, 1, 0, 'C', 1);
            $this->SetFont('', 'B');
            $col->getHeaderCell()->addToPdf($this);
            $this->SetFont('');
        }
    }

    public function simpleTable($columnSet) {
        // Colors, line width and bold font
        $this->SetFillColor(255, 0, 0);
        $this->SetTextColor(0, 0, 0);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        
        // Header
        $w = array(40, 35, 40, 45);

        $this->simpleTableHeader($columnSet);
        
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
        // Data
        $fill = 0;
        
//        var_dump(count($columnSet[0]->data));
//        die();
        
        $keys = array_keys($columnSet);
        $dataCount = count($columnSet[$keys[0]]->data);
        
        for($i=0; $i<$dataCount; $i++){
            foreach($columnSet as $col){
                $col->getBodyCell()->addToPdf($this);
                $col->nextData();
            }
            $this->Ln();
//            if($i%20 == 0 && $i != 0){
//                $this->AddPage();
//            }
            if($this->checkPageBreak(20)){
                $this->simpleTableHeader($columnSet);
                $this->Ln();
            }
        }
        
        
//        foreach($data as $row) {
//            
//            
//            
//            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
//            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
//            $this->Cell($w[2], 6, number_format($row[2]), 'LR', 0, 'R', $fill);
//            $this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
//            $this->Ln();
//            $fill=!$fill;
//        }
//        $this->Cell(array_sum($w), 0, '', 'T');
    }
    
    public function outputt($name){
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: public, must-revalidate, max-age=0');
        header('Pragma: public');
        header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream', false);
        header('Content-Type: application/download', false);
        header('Content-Type: application/pdf', false);
        header('Content-disposition: attachment; filename="'.$name.'"');
        
        $this->Output($name, 'D');
    }
}
